#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>

typedef struct FILE_ MyFILE;

struct FILE_
{
  int fd;
  char buffer[1024];
  int end;
};

MyFILE* _fopen(const char* pathname, const char* mode)
{
  assert(pathname);
  assert(mode);
  MyFILE* fp = NULL;
  if(strcmp(mode,"r")==0)
  {

  }
  else if(strcmp(mode,"r+") == 0)
  {

  }
  else if(strcmp(mode,"w") == 0)
  {
    int fd = open(pathname,O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if(fd > 0)
    {
      fp = (MyFILE*)malloc(sizeof(MyFILE));
      fp->fd = fd;
    }
  }
  else if(strcmp(mode,"w+") == 0)
  {

  }
  else if(strcmp(mode,"a") == 0)
  {

  }
  else if(strcmp(mode,"a+") == 0)
  {

  }
  else
  {
  
  }
    return fp;
}

void _fputs(const char* s, MyFILE* fp)
{
  strcpy(fp->buffer+fp->end,s);
  write(fp->fd, s, strlen(s));

}

void _fflush(MyFILE* fp)
{

}
void _fclose(MyFILE* fp)
{

}
int main()
{
  //模拟实现C语言中缓冲区的作用
  MyFILE* fp = _fopen("log.txt","w"); 
  _fputs("hello _fputs", fp);
  _fputs("hello _fputs", fp);
  _fputs("hello _fputs", fp);
  _fputs("hello _fputs", fp);
  _fputs("hello _fputs", fp);






  _fclose()





//测试缓冲区代码
//  printf("hello printf\n");
//  fprintf(stdout,"hello fprintf\n");
//  const char*  str = "hello fgets\n";
//  fputs(str, stdout);
//  
//  char buf[64] = "hello write\n";
//  write(1, buf, strlen(buf));
//  fork();
  return 0;
}
